package cn.lastcrdg.testlrslide;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/12/12 14:22
 * description：copy
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements SlidingView.IonSlidingButtonListener {
    public static final String TAG = MyAdapter.class.getSimpleName();

    Context mContext;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;

    private List<String> mDatas = new ArrayList<>();

    private SlidingView mMenu = null;

    public MyAdapter(Context context, ArrayList<String> date) {
        mContext = context;
        this.mDatas = date;
        mIDeleteBtnClickListener = (IonSlidingViewClickListener) context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_slide_item, parent, false);
        return new MyViewHolder(view);
    }

    boolean allopen = false;

    public void setAllopen(boolean allopen) {
        this.allopen = allopen;

    }

    private int getScreenWidth(Context context){
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.slidingView.setSlidingButtonListener(MyAdapter.this);

        holder.textView.setText(mDatas.get(position));

        //设置内容布局的宽为屏幕宽度
        holder.layout_content.getLayoutParams().width = getScreenWidth(mContext) + holder.rl_left.getLayoutParams().width;
//        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //判断是否有删除菜单打开
////                if (menuIsOpen()) {
////                    closeMenu();//关闭菜单
////                } else {
////                    int n = viewHolder.getLayoutPosition();
////                    mIDeleteBtnClickListener.onItemClick(v, n);
////                }
//
//            }
//        });

        holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = holder.getLayoutPosition();
                mIDeleteBtnClickListener.onDeleteBtnCilck(v, n);
            }
        });
        if (allopen) {
            holder.slidingView.openMenu();
            holder.slidingView.setCanTouch(false);
        } else {
            holder.slidingView.closeMenu();
            holder.slidingView.setCanTouch(true);
        }

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 删除菜单打开信息接收
     */
    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingView) view;
    }

    /**
     * 滑动或者点击了Item监听
     *
     * @param slidingButtonView
     */
    @Override
    public void onDownOrMove(SlidingView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    /**
     * 判断是否有菜单打开
     */
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDeleteBtnCilck(View view, int position);
    }

    public void addData(int position) {
        mDatas.add(position, "添加项");
        notifyItemInserted(position);
    }

    public void removeData(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);

    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout layout_content;
        private RelativeLayout rl_left;
        private TextView textView;
        private TextView btn_Delete;
        private SlidingView slidingView;

        MyViewHolder(View view) {
            super(view);
            slidingView = (SlidingView) view;
            btn_Delete = view.findViewById(R.id.tv_delete);
           textView = view.findViewById(R.id.text);
            rl_left = view.findViewById(R.id.rl_left);
            layout_content = view.findViewById(R.id.layout_content);
        }
    }
}
